package com.study.account.config;

import com.alibaba.druid.pool.DruidDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
//import com.alibaba.fescar.rm.datasource.DataSourceProxy;

/**
 * @author
 */
@Slf4j
@Configuration
public class DatabaseConfiguration {

	private final ApplicationContext applicationContext;

	public DatabaseConfiguration(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}


	@Bean
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSource druidDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		return druidDataSource;
	}


//	@Bean(initMethod = "init", destroyMethod = "close")
//	public DruidDataSource storageDataSource() throws SQLException {
//		Environment environment = applicationContext.getEnvironment();
//		String ip = environment.getProperty("mysql.server.ip");
//		String port = environment.getProperty("mysql.server.port");
//		String dbName = environment.getProperty("mysql.db.name");
//
//		String userName = environment.getProperty("mysql.user.name");
//		String password = environment.getProperty("mysql.user.password");
//
//		DruidDataSource druidDataSource = new DruidDataSource();
//		druidDataSource.setUrl("jdbc:mysql://" + ip + ":" + port + "/" + dbName+"?serverTimezone=GMT%2B8");
//		druidDataSource.setUsername(userName);
//		druidDataSource.setPassword(password);
////		druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
//		druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//		druidDataSource.setInitialSize(0);
//		druidDataSource.setMaxActive(180);
//		druidDataSource.setMaxWait(60000);
//		druidDataSource.setMinIdle(0);
//		druidDataSource.setValidationQuery("Select  'x' from DUAL");
//		druidDataSource.setTestOnBorrow(false);
//		druidDataSource.setTestOnReturn(false);
//		druidDataSource.setTestWhileIdle(true);
//		druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
//		druidDataSource.setMinEvictableIdleTimeMillis(25200000);
//		druidDataSource.setRemoveAbandoned(true);
//		druidDataSource.setRemoveAbandonedTimeout(1800);
//		druidDataSource.setLogAbandoned(true);
//		druidDataSource.setFilters("mergeStat");
//		return druidDataSource;
//	}

	@Primary
	@Bean("dataSource")
	public DataSourceProxy dataSource(DataSource druidDataSource) {
		return new DataSourceProxy(druidDataSource);
	}

	@Bean
	public JdbcTemplate jdbcTemplate(DataSourceProxy dataSourceProxy) {
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceProxy);
		log.info("process sql,{}",dataSourceProxy);
		jdbcTemplate.update("delete from account_tbl where user_id = 'U100001'");
		jdbcTemplate.update("insert into account_tbl(user_id, money) values ('U100001', 10000)");

		return jdbcTemplate;
	}

}
